﻿using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyNhaHang.GUI.OrderOption
{
    public partial class LocationMng : Form
    {
        String valueSelected = null;
        Bussiness.BusOptionLocation busLocation;

        GUI.Order order = null;

        public LocationMng(GUI.Order f2)
        {
            order = f2;
            InitializeComponent();
        }

        private void LocationMng_Load(object sender, EventArgs e)
        {
            busLocation = new Bussiness.BusOptionLocation(this);
            busLocation.optionLoadLocation();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (busLocation.optionNewLocation(txtLocation.Text.Trim()))
            {
                order.loadLeftTable();
                txtLocation.Text = "";
            }
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            // cell values changed
            String valueNow = gridView1.GetRowCellValue(e.RowHandle, "TEN_KHUVUC").ToString();

            if (busLocation.validateLocation(valueNow))
            {
                DialogResult result = MessageBox.Show("Bạn có muốn thay đổi tên khu vực \"" + valueSelected + "\" thành \"" + valueNow + "\"", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                // hỏi có đồng ý không, nếu yes thì...
                if (result == DialogResult.Yes)
                {
                    busLocation.optionUpdate(valueSelected, gridView1.GetRowCellValue(e.RowHandle, "TEN_KHUVUC").ToString());
                    // Cap nhat Order Left Table
                    this.order.loadLeftTable();
                }
            }
            gridColumn2.OptionsColumn.AllowEdit = false;
            busLocation.optionLoadLocation();
        }
    
        private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            // Get first click and save it to Global valueSelected
            valueSelected = this.gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_KHUVUC").ToString();
        }

        private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;

            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                contextMenuStrip1.Show(view.GridControl, e.Point);
            }
        }

        private void chỉnhSửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //gridColumn1.ColumnEdit.e = true;
            gridColumn2.OptionsColumn.AllowEdit = true;
            gridView1.FocusedRowHandle = gridView1.FocusedRowHandle;
            gridView1.FocusedColumn = gridView1.VisibleColumns[1];
            gridView1.ShowEditor();
        }

        private void xóaKhuVựcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("CẢNH BÁO: Khi xóa khu vực hệ thống sẽ tiến hành xóa toàn bộ dữ liệu về các bàn thuộc khu vực!!!\n\nBạn có muốn xóa khu vực này!!!",
                "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                String s = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_KHUVUC").ToString();
                busLocation.optionDelete(s);
                order.loadLeftTable();
                MessageBox.Show("Xóa thành công!!");
            }
            
        }
    }
}
